﻿using MediatR;
using T.ApprovalRecord;
using T.Materials.API.Application.Command.MaterialStatisticsCommand;
using T.Materials.API.Application.Command.MaterialStoreCommand;
using T.Materials.Domain.Model;
using T.Materials.Repository.MaterialsImpl;
using T.Materials.Repository.MaterialsInterface;
using static T.Materials.Domain.Enum.Enums;

namespace T.Materials.API.Application.CommandHandler.MaterialRequisitionCommandHandler
{
    public class MaterialStatisticsApproveCommandHandler : IRequestHandler<MaterialStatisticsApproveCommand, int>
    {
        /// <summary>
        /// 物资领用仓储接口
        /// </summary>
        protected readonly IMaterialRequisitionRepository materialRequisitionRepository;

        /// <summary>
        /// 
        /// </summary>
        protected readonly IMApprow mApprow;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="materialRequisitionRepository">物资领用仓储接口</param>
        /// <param name="mApprow"></param>
        public MaterialStatisticsApproveCommandHandler(IMaterialRequisitionRepository materialRequisitionRepository, IMApprow mApprow)
        {
            this.materialRequisitionRepository = materialRequisitionRepository;
            this.mApprow = mApprow;
        }

        /// <summary>
        /// 处理审批命令
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<int> Handle(MaterialStatisticsApproveCommand request, CancellationToken cancellationToken)
        {
            var res = await materialRequisitionRepository.GetModelAsync(request.MaterialRequisitionId);
            res.ApprovalStatus = request.ApprovalStatus;
            res.Approver = request.Approver;

            MApproval mApproval;
            if (request.ApprovalStatus == ApprovalStatus.已通过)
            {
                mApproval = new MApproval
                {
                    MaterialRequisitionId = request.MaterialRequisitionId,
                    Approver = request.Approver,
                    CompanyID = request.CompanyID,
                    User_Id = request.User_Id,
                    ApprovalResult = "同意",
                    ApprovalNotes="通过",
                    Subgrantor = "",
                    CreatedBy = request.CreatedBy,
                    CreatedDate = DateTime.Now
                };
            }
            else if (request.ApprovalStatus == ApprovalStatus.已拒绝)
            {
                mApproval = new MApproval
                {
                    MaterialRequisitionId = request.MaterialRequisitionId,
                    Approver = request.Approver,
                    CompanyID = request.CompanyID,
                    User_Id = request.User_Id,
                    ApprovalResult = "拒绝",
                    ApprovalNotes = "拒绝",
                    Subgrantor = "",
                    CreatedBy = request.CreatedBy,
                    CreatedDate = DateTime.Now
                };
            }
            else
            {
                // 处理其他状态（如果需要）
                throw new InvalidOperationException("未知的审批状态");
            }

            //保存审批记录
            await mApprow.AddAsync(mApproval);

            return await materialRequisitionRepository.UpdateAsync(res);
        }
    }
 }
